/**
 *
 *
 *
 */
export default {
  cartCount(state) {
    return state.cartList.length
  },
  isAdmin(state) {
    return state.userInfo.role === 1
  },
  cascaderCategories(state) {
    let categories = state.categories
    let map = {}
    categories.forEach(item => {
      let {id, parentId} = item
      if (parentId) {
        if (!map[parentId]) {
          map[parentId] = {
            id: parentId,
            children: []
          }
        }
        if (!Array.isArray(map[parentId].children)) {
          map[parentId].children = []
        }
        map[parentId].children.push(item)
      } else {
        let children = null
        if (map[id]) {
          children = map[id].children
        }
        map[id] = {
          ...item,
          children
        }
      }
    })
    let list = Object.keys(map).map(key => map[key])
    list.sort((a, b) => b.id - a.id)
    list.unshift({
      id: '',
      name: '全部'
    })
    return list
  }
}
